﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;

namespace FMS.Formulation.Licensing.Engine
{

    internal class WMINetworkAdapterInfo
    {
        private const string WIN32_NETWORKADAPTER = "Win32_NetworkAdapter";

        private const string ADAPTERTYPE = "AdapterType";

        private const string DESCRIPTION = "Description";

        private const string MACADDRESS = "MACAddress";

        private int _id = 0;

        private string adapterType = string.Empty;

        private string adapterDescription = string.Empty;

        private string macAddress = string.Empty;

        public int ID => _id;

        public string AdapterType => adapterType;

        public string Description => adapterDescription;

        public string MACAddress => macAddress;

        public string Read()
        {
            WqlObjectQuery query = new WqlObjectQuery("SELECT MACAddress FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled=true");
            ManagementScope scope = new ManagementScope("\\root\\cimv2");
            ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher(scope, query);
            try
            {
                ManagementObjectCollection managementObjectCollection = managementObjectSearcher.Get();
                foreach (ManagementObject item in managementObjectCollection)
                {
                    if (item.Properties["MACAddress"].Value != null)
                    {
                        macAddress = item.Properties["MACAddress"].Value.ToString();
                    }
                }
            }
            catch
            {
            }
            return macAddress;
        }
    }

}
